package com.sphd.Restaurant.fragments.cadrsandcouponsFragments;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.activity.IntegralActivity;
import com.sphd.Restaurant.base.LazyLoadFragment;
import com.sphd.Restaurant.infos.IntegralInfo;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.presenter.IntegralP;
import com.sphd.Restaurant.utils.Constants;
import com.sphd.Restaurant.views.IntegralView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hu on 2015/12/7.
 */
public class IntegralFragment extends LazyLoadFragment implements IntegralView {


    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_srf)
    SwipeRefreshLayout mySrf;
    IntegralP integrap;
    BaseQuickAdapter<IntegralInfo> baseQuickAdapter;
    List<IntegralInfo> integraList;

    @Override
    public int getLayout() {
        return R.layout.fragment_coupons_cards;
    }

    @Override
    public void initViews(View view) {
        ButterKnife.bind(this, view);
        initView();
    }

    @Override
    public void loadData() {
        if (getBoolean(Constants.ISLOGIN))
            addData();
    }

    private void initView() {
        setRefresh(mySrf);
        setRecycle(myRecy);
        initAdapter();
        initEvent();

    }

    private void initEvent() {
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString(Constants.TAG, context.getClass().getName());
                bundle.putSerializable(Constants.INTEGRAL, (IntegralInfo) baseQuickAdapter.getItem(position));
                startActivity(IntegralActivity.class, bundle);
            }
        });

        mySrf.setOnRefreshListener(() -> {
            if (getBoolean(Constants.ISLOGIN)) {
                addData();
            } else {
                mySrf.setRefreshing(false);
            }
        });
    }

    private void addData() {
        integrap.addData(Model.HTTPURL + Model.INTEGRAL + "phone=" + getString(Constants.USERPHONE));
    }

    private void initAdapter() {
        integrap = new IntegralP(this);
        integraList = new ArrayList<>();
        baseQuickAdapter = new BaseQuickAdapter<IntegralInfo>(R.layout.item_fragment_cardsandcoupons, integraList) {
            @Override
            protected void convert(BaseViewHolder helper, IntegralInfo item) {

                helper.setText(R.id.tv_most_lift, item.getMem_cgname())
                        .setText(R.id.tv_most_right, "剩余积分:" + item.getMem_integral());
            }
        };
        myRecy.setAdapter(baseQuickAdapter);
    }


    @Override
    public void sendList(List<IntegralInfo> integralInfos) {
        baseQuickAdapter.setNewData(integralInfos);
       addFootView(baseQuickAdapter);
        setLoadDataCompleted(true);
    }

    @Override
    public void showdialog() {
        showDialog();
    }

    @Override
    public void dissmissdialog() {
        dissMissDialog();
        mySrf.setRefreshing(false);
    }
}
